package com.indexingsystem.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.indexingsystem.boss.Vo.MagazinesDetailsVo;
import com.indexingsystem.boss.Vo.MagazinesQueryVo;
import com.indexingsystem.boss.Vo.MagazinesReqVo;
import com.indexingsystem.boss.Vo.MagazinesTableVo;
import com.indexingsystem.boss.Vo.OrderExportMagazinesQueryVo;
import com.indexingsystem.boss.Vo.OrderMagazinesVo;
import com.indexingsystem.boss.Vo.ResCategoryRelationsVo;
import com.indexingsystem.boss.Vo.ResourcesVo;
import com.indexingsystem.boss.Vo.SpecialResourceDatasVo;
import com.indexingsystem.boss.admin.mapper.ArticleContentsMapper;
import com.indexingsystem.boss.admin.mapper.ArticlesMapper;
import com.indexingsystem.boss.admin.mapper.AuditLogsMapper;
import com.indexingsystem.boss.admin.mapper.DhMagazinesMlMapper;
import com.indexingsystem.boss.admin.mapper.DhResourceauditlogsMapper;
import com.indexingsystem.boss.admin.mapper.MagazinesMapper;
import com.indexingsystem.boss.admin.mapper.ResCategoryRelationsMapper;
import com.indexingsystem.boss.admin.mapper.ResourceRelationsMapper;
import com.indexingsystem.boss.admin.mapper.ResourceTypeMapper;
import com.indexingsystem.boss.admin.mapper.ResourcesMapper;
import com.indexingsystem.boss.admin.mapper.SpecialResourceDatasMapper;
import com.indexingsystem.boss.common.PagedResult;
import com.indexingsystem.boss.entity.ArticleContents;
import com.indexingsystem.boss.entity.Articles;
import com.indexingsystem.boss.entity.AuditLogs;
import com.indexingsystem.boss.entity.DhMagazinesMl;
import com.indexingsystem.boss.entity.DhResourceauditlogs;
import com.indexingsystem.boss.entity.DhTodayStorageInfos;
import com.indexingsystem.boss.entity.Magazines;
import com.indexingsystem.boss.entity.ResCategoryRelations;
import com.indexingsystem.boss.entity.ResourceCategors;
import com.indexingsystem.boss.entity.ResourceCategoryRelations;
import com.indexingsystem.boss.entity.ResourceRelations;
import com.indexingsystem.boss.entity.ResourceType;
import com.indexingsystem.boss.entity.ResourceUpdateRecord;
import com.indexingsystem.boss.entity.Resources;
import com.indexingsystem.boss.entity.SpecialResourceDatas;
import com.indexingsystem.boss.utils.BeanUtils;
import com.indexingsystem.boss.utils.DateUtil;
import com.indexingsystem.boss.utils.InstitutionLoadUtils;
import com.indexingsystem.boss.utils.StringUtil;
import com.indexingsystem.db.mongoDB.impl.ArticlesResourceMongo;
import com.indexingsystem.db.mongoDB.impl.MagazinesMongo;
import com.indexingsystem.db.mongoDB.impl.ResourceCategorsResListMongo;
import com.indexingsystem.db.mongoDB.po.MagazinesListPo;
import com.indexingsystem.db.mongoDB.po.MagazinesResourceMongoVo;
import com.indexingsystem.db.mongoDB.po.ResourceCategorsResPo;
import com.indexingsystem.system.service.IMagazinesService;
import com.indexingsystem.system.service.IResourceCategorsService;
import com.indexingsystem.system.service.IResourceService;
import com.indexingsystem.system.service.IResourceUpdateRecordService;
import com.indexingsystem.system.service.IdhTodayStorageInfosService;

@Service
public class MagazinesServiceImpl implements IMagazinesService {

	@Resource
	private MagazinesMapper					magazinesMapper;

	@Resource
	private ResourcesMapper					resourcesMapper;

	@Resource
	private ResCategoryRelationsMapper		resCategoryRelationsMapper;

	@Resource
	private SpecialResourceDatasMapper		specialResourceDatasMapper;

	@Resource
	private AuditLogsMapper					auditLogsMapper;

	@Resource
	private IdhTodayStorageInfosService		todayStorageInfosDao;

	@Resource
	private ResourceTypeMapper				resourceTypeMapper;

	@Resource
	private ArticleContentsMapper			articleContentsMapper;

	@Resource
	private ResourceRelationsMapper			resourceRelationsMapper;

	@Resource
	private DhMagazinesMlMapper				dhMagazinesMlMapper;

	@Resource
	private ArticlesMapper					articlesMapper;

	@Resource
	private IResourceService				iResourceService;

	@Resource
	private DhResourceauditlogsMapper		dhResourceauditlogsMapper;

	@Resource
	private IResourceUpdateRecordService	iResourceUpdateRecordService;

	@Resource
	private IResourceCategorsService		iResourceCategorsService;

	@Override
	public int deleteByPrimaryKey(String resId) {
		return magazinesMapper.deleteByPrimaryKey( resId );
	}

	@Override
	public int insertSelective(Magazines record) {
		return magazinesMapper.insertSelective( record );
	}

	@Override
	public Magazines selectByPrimaryKey(String resId) {
		return magazinesMapper.selectByPrimaryKey( resId );
	}

	@Override
	public int updateByPrimaryKeySelective(Magazines record) {
		return magazinesMapper.updateByPrimaryKeySelective( record );
	}

	/**
	 * 通过资源分类id和订购的开始年份及结束年份
	 * 
	 * @param magazinesYearListReqVo
	 * @return
	 */
	@Override
	public List<Magazines> getMagazinesListByResCategoryIdOrYear(OrderMagazinesVo orderMagazinesVo) {
		return magazinesMapper.getMagazinesListByResCategoryIdOrYear( orderMagazinesVo );
	}

	/**
	 * 获取期刊总数量.
	 * 
	 * @param magazinesQueryVo
	 * @return
	 */
	@Override
	public Integer getMagazinesListCount(MagazinesQueryVo magazinesQueryVo) {

		Integer maxCount = 0;
		maxCount = MagazinesMongo.findMagazinesMaxCount( magazinesQueryVo, InstitutionLoadUtils.getString( "institutionId" ) );
		if (maxCount == 0) {
			maxCount = magazinesMapper.getMagazinesListCount( magazinesQueryVo );
		}
		return maxCount;
	}

	/**
	 * 新增期刊信息.
	 * 
	 * @param MagazinesReqVo
	 * @return
	 */
	@Override
	public int addMagazinesInfo(MagazinesReqVo MagazinesReqVo, AuditLogs auditLogs, DhTodayStorageInfos todayStorageInfos) throws Exception {
		int result = 0;
		if (null != MagazinesReqVo) {
			List<MagazinesResourceMongoVo> magazinesResourceMongoVoList = new ArrayList<MagazinesResourceMongoVo>();
			MagazinesResourceMongoVo magazinesResourceMongoVo = new MagazinesResourceMongoVo();
			Resources resources = MagazinesReqVo.getResources();
			if (null != resources) {
				magazinesResourceMongoVo.setResId( resources.getResId() );
				magazinesResourceMongoVo.setResTypeId( resources.getResTypeId() );
				magazinesResourceMongoVo.setResTable( "Magazines" );
				resourcesMapper.insertSelective( resources );
				magazinesResourceMongoVo.setResources( resources );
			}
			Magazines magazines = MagazinesReqVo.getMagazines();
			if (null != magazines) {
				result = magazinesMapper.insertSelective( magazines );
				magazinesResourceMongoVo.setMagazines( magazines );
			}
			ResCategoryRelations resCategoryRelation = null;
			List<ResCategoryRelations> resultResCategoryRelationsList = null;

			List<ResCategoryRelationsVo> resCategoryRelationsVoList = MagazinesReqVo.getResCategoryRelationsList();
			if (null != resCategoryRelationsVoList && resCategoryRelationsVoList.size() > 0) {
				resultResCategoryRelationsList = new ArrayList<ResCategoryRelations>();
				for (ResCategoryRelationsVo resCategoryRela : resCategoryRelationsVoList) {
					resCategoryRelation = new ResCategoryRelations();
					resCategoryRelation.setResCategoryRelationId( resCategoryRela.getResCategoryRelationId() );
					resCategoryRelation.setResCategoryId( resCategoryRela.getResCategoryId() );
					resCategoryRelation.setResId( resCategoryRela.getResId() );

					if (!resultResCategoryRelationsList.contains( resCategoryRelation )) {
						resultResCategoryRelationsList.add( resCategoryRelation );
					}
				}
			}
			if (null != resultResCategoryRelationsList && resultResCategoryRelationsList.size() > 0) {
				resCategoryRelationsMapper.insertResCategoryRelationsBatch( resultResCategoryRelationsList );

				List<ResCategoryRelations> resCategoryRelationsList = new ArrayList<ResCategoryRelations>();
				for (ResCategoryRelationsVo resCategoryRela : resCategoryRelationsVoList) {
					// 登记资源分类资源变更表
					// 记录接触关系的资源
					ResourceUpdateRecord resourceUpdateRecord = new ResourceUpdateRecord();
					resourceUpdateRecord.setResourceUpdateRecordId( StringUtil.createSystemDataPrimaryKey() );
					resourceUpdateRecord.setResId( resCategoryRela.getResId() );
					ResourceType resType = resourceTypeMapper.selectResourceTypeResTableExits( "Magazines" );
					resourceUpdateRecord.setResTable( resType.getResTable() );
					resourceUpdateRecord.setResTypeId( resType.getResTypeId() );
					resourceUpdateRecord.setSuperCategoryId( resCategoryRela.getParentId() );
					resourceUpdateRecord.setResourceCategoryId( resCategoryRela.getResCategoryId() );
					resourceUpdateRecord.setOperatedate( DateUtil.getCurDate_YYYY_MM_DD() );
					resourceUpdateRecord.setState( "1" );
					iResourceUpdateRecordService.insert( resourceUpdateRecord );
				}

				// 资源分类关系
				magazinesResourceMongoVo.setResCategoryRelationsList( resultResCategoryRelationsList );

				// 报刊缓存mongo
				List<ResourceCategorsResPo> resourceCategorsResPoList = null;
				if (null != resCategoryRelationsList && resCategoryRelationsList.size() > 0) {
					resourceCategorsResPoList = new ArrayList<ResourceCategorsResPo>();
					for (ResCategoryRelations resCategoryRelations : resCategoryRelationsList) {
						ResourceCategorsResPo resourceCategorsResPo = new ResourceCategorsResPo();
						resourceCategorsResPo.setThumbnail( magazines.getFrontCover() );
						resourceCategorsResPo.setResType( 1 );
						String publishDate = magazines.getYear() + "年" + magazines.getMonth() + "月" + "第" + magazines.getPeriodOfTime() + "期";

						String month = "";
						if (magazines.getMonth() < 10) {
							month = "0" + magazines.getMonth().toString();
							// dd = "01";
						} else {
							month = magazines.getMonth().toString();
						}

						String dateValue = magazines.getYear() + "-" + month + "-" + "01";
						resourceCategorsResPo.setDatePx( DateUtil.parseFormatToPublishDate( dateValue ) );
						resourceCategorsResPo.setPublishDate( publishDate );

						resourceCategorsResPo.setResId( resCategoryRelations.getResId() );
						resourceCategorsResPo.setResName( resources.getResName() );
						resourceCategorsResPo.setPublishDate( resources.getPublishDate() );
						resourceCategorsResPo.setResTypeId( resources.getResTypeId() );
						resourceCategorsResPo.setResCategoryId( resCategoryRelations.getResCategoryId() );
						resourceCategorsResPo.setStatus( resources.getStatus() );

						// 通过资源分类id获取资源分类名称
						ResourceCategors resourceCategors = iResourceCategorsService.selectByPrimaryKey( resCategoryRelations.getResCategoryId() );
						resourceCategorsResPo.setResCategoryName( resourceCategors.getResName() );
						resourceCategorsResPo.setResTable( "Magazines" );

						if (!resourceCategorsResPoList.contains( resourceCategorsResPo )) {
							resourceCategorsResPoList.add( resourceCategorsResPo );
						}
						ResourceCategorsResListMongo.addResourceCategorsResList( resourceCategorsResPoList, InstitutionLoadUtils.getString( "institutionId" ) );
					}
				}
			}
			List<SpecialResourceDatas> specialResourceDatasList = MagazinesReqVo.getSpecialResourceDatasList();
			if (null != specialResourceDatasList && specialResourceDatasList.size() > 0) {
				specialResourceDatasMapper.batchAddSpecialResourceDatas( specialResourceDatasList );
				magazinesResourceMongoVo.setDhSpecialResourceDatasList( specialResourceDatasList );
			}
			auditLogsMapper.insertSelective( auditLogs );
			magazinesResourceMongoVo.setAuditLogs( auditLogs );

			todayStorageInfosDao.insert( todayStorageInfos );
			magazinesResourceMongoVo.setTodayStorageInfos( todayStorageInfos );

			DhResourceauditlogs resourceauditlogs = new DhResourceauditlogs();
			// 增加审核信息
			resourceauditlogs.setCreatedate( DateUtil.parseFormatToDate( DateUtil.getCurTotalDate() ) );
			resourceauditlogs.setHandler( auditLogs.getHandler() == null ? "admin" : auditLogs.getHandler() );
			resourceauditlogs.setResid( magazines.getResId() );
			resourceauditlogs.setId( StringUtil.createSystemDataPrimaryKey() );
			resourceauditlogs.setStatus( 0 );
			resourceauditlogs.setDescription( "新增报刊入库" );
			dhResourceauditlogsMapper.insert( resourceauditlogs );

			magazinesResourceMongoVo.setResourceauditlogs( resourceauditlogs );

			// 根据报刊ID查询文章列表 保存列表中的res_id集合到mongo
			List<String> articleResIds = new ArrayList<String>();
			List<Resources> queryArticlesList = resourcesMapper.getOrderResourcesListByMagazinesId( magazines.getResId() );
			if (null != queryArticlesList && queryArticlesList.size() > 0) {
				for (Resources articles : queryArticlesList)
					articleResIds.add( articles.getResId() );
			}
			// 文章的资源ID集合
			magazinesResourceMongoVo.setArticeIds( articleResIds );

			// 资源关系
			List<ResourceRelations> queryResRelationsList = this.resourceRelationsMapper.getResourceRelationsListByResId( magazines.getResId() );
			magazinesResourceMongoVo.setDhResourceRelationsList( queryResRelationsList );

			magazinesResourceMongoVoList.add( magazinesResourceMongoVo );
			MagazinesMongo.addMagazines( magazinesResourceMongoVoList, InstitutionLoadUtils.getString( "institutionId" ) );
		}
		return result;
	}

	/**
	 * 修改期刊信息.
	 * 
	 * @param MagazinesReqVo
	 * @return
	 */
	@Override
	public int updateMagazinesInfo(MagazinesReqVo MagazinesReqVo) throws Exception {
		int result = 0;
		if (null != MagazinesReqVo) {
			MagazinesResourceMongoVo magazinesResourceMongoVo = new MagazinesResourceMongoVo();
			Resources resources = MagazinesReqVo.getResources();
			if (null != resources) {

				magazinesResourceMongoVo.setResId( resources.getResId() );
				magazinesResourceMongoVo.setResTypeId( resources.getResTypeId() );
				magazinesResourceMongoVo.setResTable( "Magazines" );

				resourcesMapper.updateByPrimaryKeySelective( resources );
				Resources queryRes = resourcesMapper.selectByPrimaryKey( resources.getResId() );
				magazinesResourceMongoVo.setResources( queryRes );
			}
			Magazines magazines = MagazinesReqVo.getMagazines();
			if (null != magazines) {
				result = magazinesMapper.updateByPrimaryKeySelective( magazines );
				magazinesResourceMongoVo.setMagazines( magazines );
			}

			ResCategoryRelations resCategoryRelation = new ResCategoryRelations();
			List<String> resIdList = null;
			List<String> resCategoryIdList = null;
			List<ResCategoryRelations> resultResCategoryRelationsList = null;
			List<ResCategoryRelationsVo> resCategoryRelationsVoList = MagazinesReqVo.getResCategoryRelationsList();
			if (null != resCategoryRelationsVoList && resCategoryRelationsVoList.size() > 0) {
				resultResCategoryRelationsList = new ArrayList<ResCategoryRelations>();
				resIdList = new ArrayList<String>();
				resCategoryIdList = new ArrayList<String>();

				for (ResCategoryRelationsVo resCategoryRela : resCategoryRelationsVoList) {
					resCategoryRelation = new ResCategoryRelations();
					resCategoryRelation.setResCategoryRelationId( StringUtil.createSystemDataPrimaryKey() );
					resCategoryRelation.setResCategoryId( resCategoryRela.getResCategoryId() );
					resCategoryRelation.setResId( resCategoryRela.getResId() );
					resCategoryIdList.add( resCategoryRela.getResCategoryId() );
					resIdList.add( resCategoryRela.getResId() );

					if (!resultResCategoryRelationsList.contains( resCategoryRelation )) {
						resultResCategoryRelationsList.add( resCategoryRelation );
					}
				}
				magazinesResourceMongoVo.setResCategoryRelationsList( resultResCategoryRelationsList );
			}
			if (null != resultResCategoryRelationsList && resultResCategoryRelationsList.size() > 0) {
				// 先清理资源所有分类关系
				List<String> delResIdList = new ArrayList<String>();
				delResIdList.add( resources.getResId() );
				ResourceCategorsResListMongo.deleteResCategoryRelationsByResId( delResIdList, InstitutionLoadUtils.getString( "institutionId" ) );

				// 重新缓存资源分类关系
				List<ResourceCategorsResPo> resourceCategorsResPoList = null;
				if (null != resultResCategoryRelationsList && resultResCategoryRelationsList.size() > 0) {
					resourceCategorsResPoList = new ArrayList<ResourceCategorsResPo>();
					for (ResCategoryRelations resCategoryRelations : resultResCategoryRelationsList) {
						ResourceCategorsResPo resourceCategorsResPo = new ResourceCategorsResPo();
						resourceCategorsResPo.setThumbnail( magazines.getFrontCover() );
						resourceCategorsResPo.setResType( 1 );
						String publishDate = magazines.getYear() + "年" + magazines.getMonth() + "月" + "第" + magazines.getPeriodOfTime() + "期";

						String month = "";
						if (magazines.getMonth() < 10) {
							month = "0" + magazines.getMonth().toString();
							// dd = "01";
						} else {
							month = magazines.getMonth().toString();
						}

						String dateValue = magazines.getYear() + "-" + month + "-" + "01";
						resourceCategorsResPo.setDatePx( DateUtil.parseFormatToPublishDate( dateValue ) );
						resourceCategorsResPo.setPublishDate( publishDate );

						resourceCategorsResPo.setResId( resCategoryRelations.getResId() );
						resourceCategorsResPo.setResName( resources.getResName() );
						resourceCategorsResPo.setPublishDate( resources.getPublishDate() );
						resourceCategorsResPo.setResTypeId( resources.getResTypeId() );
						resourceCategorsResPo.setResCategoryId( resCategoryRelations.getResCategoryId() );
						resourceCategorsResPo.setStatus( resources.getStatus() );

						// 通过资源分类id获取资源分类名称
						ResourceCategors resourceCategors = iResourceCategorsService.selectByPrimaryKey( resCategoryRelations.getResCategoryId() );
						resourceCategorsResPo.setResCategoryName( resourceCategors.getResName() );
						resourceCategorsResPo.setResTable( "Magazines" );

						if (!resourceCategorsResPoList.contains( resourceCategorsResPo )) {
							resourceCategorsResPoList.add( resourceCategorsResPo );
						}
						ResourceCategorsResListMongo.addResourceCategorsResList( resourceCategorsResPoList, InstitutionLoadUtils.getString( "institutionId" ) );
					}
				}

				if (null != resIdList && resIdList.size() > 0) {
					// 先删后加
					resCategoryRelationsMapper.deleteResBatchCategoryRelationsByResId( resIdList );
				}
				// 后加
				resCategoryRelationsMapper.insertResCategoryRelationsBatch( resultResCategoryRelationsList );

				for (ResCategoryRelations resCategoryRelations : resultResCategoryRelationsList) {
					// 登记资源分类资源变更表
					// 记录接触关系的资源
					ResourceUpdateRecord resourceUpdateRecord = new ResourceUpdateRecord();
					resourceUpdateRecord.setResourceUpdateRecordId( StringUtil.createSystemDataPrimaryKey() );
					resourceUpdateRecord.setResId( resCategoryRelations.getResId() );
					ResourceType resType = resourceTypeMapper.selectResourceTypeResTableExits( "Magazines" );
					resourceUpdateRecord.setResTable( resType.getResTable() );
					resourceUpdateRecord.setResTypeId( resType.getResTypeId() );
					ResourceCategors resourceCategors = iResourceCategorsService.selectByPrimaryKey( resCategoryRelations.getResCategoryId() );
					resourceUpdateRecord.setSuperCategoryId( resourceCategors.getParentId() );
					resourceUpdateRecord.setResourceCategoryId( resCategoryRelations.getResCategoryId() );
					resourceUpdateRecord.setOperatedate( DateUtil.getCurDate_YYYY_MM_DD() );
					resourceUpdateRecord.setState( "1" );
					iResourceUpdateRecordService.insert( resourceUpdateRecord );
				}
			}

			List<SpecialResourceDatas> specialResourceDatasList = MagazinesReqVo.getSpecialResourceDatasList();
			if (null != specialResourceDatasList && specialResourceDatasList.size() > 0) {
				if (null != resIdList && resIdList.size() > 0) {
					// 终端先删后加
					specialResourceDatasMapper.deleteSpecialResourceDatasByIdBatch( resIdList );
				}
				specialResourceDatasMapper.batchAddSpecialResourceDatas( specialResourceDatasList );
			}

			magazinesResourceMongoVo.setDhSpecialResourceDatasList( specialResourceDatasList );

			// 根据报刊ID查询文章列表 保存列表中的res_id集合到mongo
			List<String> articleResIds = new ArrayList<String>();
			List<Resources> queryArticlesList = resourcesMapper.getOrderResourcesListByMagazinesId( magazines.getResId() );
			if (null != queryArticlesList && queryArticlesList.size() > 0) {
				for (Resources articles : queryArticlesList)
					articleResIds.add( articles.getResId() );
			}
			// 文章的资源ID集合
			magazinesResourceMongoVo.setArticeIds( articleResIds );

			// 资源关系
			List<ResourceRelations> queryResRelationsList = this.resourceRelationsMapper.getResourceRelationsListByResId( magazines.getResId() );
			magazinesResourceMongoVo.setDhResourceRelationsList( queryResRelationsList );

			MagazinesMongo.updateMagazines( magazinesResourceMongoVo, InstitutionLoadUtils.getString( "institutionId" ) );
		}

		return result;
	}

	@Override
	public PagedResult<MagazinesTableVo> selectMagazinesList(MagazinesQueryVo magazinesQueryVo) {
		Integer pageNo = magazinesQueryVo.getPageNumber() == null ? 1 : magazinesQueryVo.getPageNumber();
		Integer pageSize = magazinesQueryVo.getPageSize() == null ? 10 : magazinesQueryVo.getPageSize();
		// startPage是告诉拦截器说我要开始分页了。分页参数是这两个。
		PageHelper.startPage( pageNo, pageSize );
		List<MagazinesTableVo> magazinesTableVosList = MagazinesMongo.findMagazinesByPageNew( magazinesQueryVo, InstitutionLoadUtils.getString( "institutionId" ) );
		if (null != magazinesTableVosList && magazinesTableVosList.size() > 0) {
			return BeanUtils.toPagedResult( magazinesTableVosList );
		} else {
			// 做测试数据
			MagazinesQueryVo magazinesQueryVoNull = new MagazinesQueryVo();
			magazinesTableVosList = magazinesMapper.selectMagazinesList( magazinesQueryVo );

			return BeanUtils.toPagedResult( magazinesTableVosList );

		}

	}

	@Override
	public MagazinesTableVo selectMagazinesById(String resId) {
		MagazinesTableVo magazinesTableVo = MagazinesMongo.findMagazinesDetailByToMagazinesTableVo( resId, InstitutionLoadUtils.getString( "institutionId" ) );
		if (null != magazinesTableVo) {

			return magazinesTableVo;
		}
		return magazinesMapper.selectMagazinesById( resId );
	}

	/**
	 * 批量删除期刊信息.
	 */
	@Override
	public int deleteMagazinesByIdBatch(List<String> resIdList) throws Exception {
		int result = 0;
		if (null != resIdList && resIdList.size() > 0) {
			for (String resId : resIdList) {
				Magazines queryMagazines = selectByPrimaryKey( resId );
				List<Resources> queryArticlesList = null;
				if (null != queryMagazines) {

					queryArticlesList = resourcesMapper.getOrderResourcesListByMagazinesId( queryMagazines.getResId() );
					// 1、删除期刊
					result += magazinesMapper.deleteByPrimaryKey( resId );

					// 第二步删除资源分类关系
					List<ResCategoryRelations> resCategoryRelationList = resCategoryRelationsMapper.getResCategoryRelationsListByResId( resId );
					for (ResCategoryRelations resCategoryRelations : resCategoryRelationList) {
						resourceRelationsMapper.deleteByPrimaryKey( resCategoryRelations.getResCategoryRelationId() );

						// 登记资源分类资源变更表
						// 记录接触关系的资源
						ResourceUpdateRecord resourceUpdateRecord = new ResourceUpdateRecord();
						resourceUpdateRecord.setResourceUpdateRecordId( StringUtil.createSystemDataPrimaryKey() );
						resourceUpdateRecord.setResId( resCategoryRelations.getResId() );
						ResourceType resType = resourceTypeMapper.selectResourceTypeResTableExits( "Magazines" );
						resourceUpdateRecord.setResTable( resType.getResTable() );
						resourceUpdateRecord.setResTypeId( resType.getResTypeId() );
						ResourceCategors resourceCategors = iResourceCategorsService.selectByPrimaryKey( resCategoryRelations.getResCategoryId() );
						resourceUpdateRecord.setSuperCategoryId( resourceCategors.getParentId() );
						resourceUpdateRecord.setResourceCategoryId( resCategoryRelations.getResCategoryId() );
						resourceUpdateRecord.setOperatedate( DateUtil.getCurDate_YYYY_MM_DD() );
						resourceUpdateRecord.setState( "0" );
						iResourceUpdateRecordService.insert( resourceUpdateRecord );
					}

					// 第三步删除资源关系
					// 资源关系列表
					List<ResourceRelations> queryResRelationsList = resourceRelationsMapper.getResourceRelationsListByResId( resId );
					for (ResourceRelations resRelations : queryResRelationsList) {
						resourceRelationsMapper.deleteByPrimaryKey( resRelations.getResRelationId() );
					}

					// 第四步：删除资源终端关系
					List<SpecialResourceDatas> tempSpecialResDataList = specialResourceDatasMapper.getSpecialResourceDataListByResId( resId );
					for (SpecialResourceDatas resSpecialDatas : tempSpecialResDataList) {
						specialResourceDatasMapper.deleteBySpecialResourceDataId( resSpecialDatas.getSpecialResDataId() );
					}
					ResourcesVo resourcesVo = new ResourcesVo();
					resourcesVo.setResId( resId );

					if (null != queryMagazines) {
						// 通过报刊id获取所有文章资源id
						/*
						 * List<Resources> queryArticlesList =
						 * resourcesMapper.getOrderResourcesListByMagazinesId
						 * (queryMagazines.getResId());
						 */
						if (null != queryArticlesList && queryArticlesList.size() > 0) {
							for (Resources articelesRes : queryArticlesList) {
								if (null != articelesRes) {
									// 添加文章列表
									Articles articles = articlesMapper.selectByPrimaryKey( articelesRes.getResId() );
									if (null != articles) {
										articlesMapper.deleteByPrimaryKey( articles.getResId() );
										// 通过期刊资源id获取终端资源列表
										if (null != articles) {
											List<SpecialResourceDatas> resTempSpecialResDataList = specialResourceDatasMapper
													.getSpecialResourceDataListByResId( articles.getResId() );
											if (null != resTempSpecialResDataList && resTempSpecialResDataList.size() > 0) {
												for (SpecialResourceDatas resSpecialDatas : resTempSpecialResDataList) {
													specialResourceDatasMapper.deleteBySpecialResourceDataId( resSpecialDatas.getSpecialResDataId() );
												}
											}

											// 获取订单文章内容表
											// 添加文章内容列表
											ArticleContents articleContents = articleContentsMapper.selectByPrimaryKey( articles.getResId() );
											if (null != articleContents) {
												articleContentsMapper.deleteByPrimaryKey( articleContents.getResId() );
											}

											// 5、删除审核日志表关系
											List<AuditLogs> auditLogsList = auditLogsMapper.getAuditLogsListByResId( articles.getResId() );
											for (AuditLogs auditLogs : auditLogsList) {
												auditLogsMapper.deleteByPrimaryKey( auditLogs.getProductLogId() );
											}

											// 清理入库审核日志表关系
											DhResourceauditlogs dhResourceAuditLogs = dhResourceauditlogsMapper.getResAuditlogsByResId( articles
													.getResId() );
											if (null != dhResourceAuditLogs) {
												dhResourceauditlogsMapper.deleteByPrimaryKey( dhResourceAuditLogs.getId() );
											}

											// 删除流水
											List<DhTodayStorageInfos> dhTodayStorageInfosList = todayStorageInfosDao
													.getDhTodayStorageInfosByResId( articles.getResId() );
											for (DhTodayStorageInfos dhTodayStorageInfos : dhTodayStorageInfosList) {
												todayStorageInfosDao.deleteTodayStorageInfoById( dhTodayStorageInfos.getStorageId() );
											}
										}

										resourcesMapper.deleteByPrimaryKey( articles.getResId() );

									}
								}
							}
						}

						// 1、删除期刊
						result += magazinesMapper.deleteByPrimaryKey( queryMagazines.getResId() );

						// 第二步删除资源分类关系
						List<ResCategoryRelations> resCategoryRelationList1 = resCategoryRelationsMapper
								.getResCategoryRelationsListByResId( queryMagazines.getResId() );
						for (ResCategoryRelations resCategoryRelations : resCategoryRelationList1) {
							resourceRelationsMapper.deleteByPrimaryKey( resCategoryRelations.getResCategoryRelationId() );

							// 登记资源分类资源变更表
							// 记录接触关系的资源
							ResourceUpdateRecord resourceUpdateRecord = new ResourceUpdateRecord();
							resourceUpdateRecord.setResourceUpdateRecordId( StringUtil.createSystemDataPrimaryKey() );
							resourceUpdateRecord.setResId( resCategoryRelations.getResId() );
							ResourceType resType = resourceTypeMapper.selectResourceTypeResTableExits( "Magazines" );
							resourceUpdateRecord.setResTable( resType.getResTable() );
							resourceUpdateRecord.setResTypeId( resType.getResTypeId() );
							ResourceCategors resourceCategors = iResourceCategorsService.selectByPrimaryKey( resCategoryRelations.getResCategoryId() );
							resourceUpdateRecord.setSuperCategoryId( resourceCategors.getParentId() );
							resourceUpdateRecord.setResourceCategoryId( resCategoryRelations.getResCategoryId() );
							resourceUpdateRecord.setOperatedate( DateUtil.getCurDate_YYYY_MM_DD() );
							resourceUpdateRecord.setState( "0" );
							iResourceUpdateRecordService.insert( resourceUpdateRecord );
						}

						// 第三步删除资源关系
						// 资源关系列表
						List<ResourceRelations> queryResRelationsList1 = resourceRelationsMapper.getResourceRelationsListByResId( queryMagazines
								.getResId() );
						for (ResourceRelations resRelations : queryResRelationsList1) {
							resourceRelationsMapper.deleteByPrimaryKey( resRelations.getResRelationId() );
						}

						// 第四步：删除资源终端关系
						List<SpecialResourceDatas> tempSpecialResDataList1 = specialResourceDatasMapper
								.getSpecialResourceDataListByResId( queryMagazines.getResId() );
						for (SpecialResourceDatas resSpecialDatas : tempSpecialResDataList1) {
							specialResourceDatasMapper.deleteBySpecialResourceDataId( resSpecialDatas.getSpecialResDataId() );
						}

						// 清除期刊文章目录表
						List<DhMagazinesMl> dhMagazinesMlList = dhMagazinesMlMapper.getDhMagazinesMlListByResId( queryMagazines.getResId() );
						for (DhMagazinesMl dhMagazinesMl : dhMagazinesMlList) {
							dhMagazinesMlMapper.deleteByPrimaryKey( dhMagazinesMl.getMlId() );
						}

						// 5、删除审核日志表关系
						List<AuditLogs> auditLogsList = auditLogsMapper.getAuditLogsListByResId( queryMagazines.getResId() );
						for (AuditLogs auditLogs : auditLogsList) {
							auditLogsMapper.deleteByPrimaryKey( auditLogs.getProductLogId() );
						}

						// 清理入库审核日志表关系
						DhResourceauditlogs dhResourceAuditLogs = dhResourceauditlogsMapper.getResAuditlogsByResId( queryMagazines.getResId() );
						if (null != dhResourceAuditLogs) {
							dhResourceauditlogsMapper.deleteByPrimaryKey( dhResourceAuditLogs.getId() );
						}

						// 删除流水
						List<DhTodayStorageInfos> dhTodayStorageInfosList = todayStorageInfosDao.getDhTodayStorageInfosByResId( queryMagazines
								.getResId() );
						for (DhTodayStorageInfos dhTodayStorageInfos : dhTodayStorageInfosList) {
							todayStorageInfosDao.deleteTodayStorageInfoById( dhTodayStorageInfos.getStorageId() );
						}

						resourcesMapper.deleteByPrimaryKey( queryMagazines.getResId() );
						List<String> ids = new ArrayList<String>();
						ids.add( queryMagazines.getResId() );
						MagazinesMongo.deleteMagazinesByIds( ids, InstitutionLoadUtils.getString( "institutionId" ) );

					}

				}

			}
			// 先清理资源所有分类关系
			ResourceCategorsResListMongo.deleteResCategoryRelationsByResId( resIdList, InstitutionLoadUtils.getString( "institutionId" ) );
		}

		return result;
	}

	/**
	 * 根据订单资源分类列表、开始年份、结束年份查询订单所有资源的期刊资源列表.
	 * 
	 * @param orderExportMagazinesQueryVo
	 * @return
	 */
	@Override
	public List<Magazines> getOrderMagazinesResListByResCategoryId(OrderExportMagazinesQueryVo orderExportMagazinesQueryVo) {
		return magazinesMapper.getOrderMagazinesResListByResCategoryId( orderExportMagazinesQueryVo );
	}

	@Override
	public void addMagazinestest() {
		/*
		 * List<MagazinesTableVo>
		 * magazinesTableVosList=magazinesMapper.selectMagazinesAllList();
		 * if(null!=magazinesTableVosList&&magazinesTableVosList.size()>0) {
		 * 
		 * List<MagazinesListPo> magazinesListPo=new
		 * ArrayList<MagazinesListPo>(); for(MagazinesTableVo
		 * magazinesTableVo:magazinesTableVosList) { MagazinesListPo
		 * magazinesPo=new MagazinesListPo(); magazinesPo.setResId(
		 * magazinesTableVo.getResId() ); magazinesPo.setMagazinesTableVo(
		 * magazinesTableVo ); Resources
		 * resources=this.resourcesMapper.selectByPrimaryKey(
		 * magazinesTableVo.getResId() ); ResourceType
		 * type=resourceTypeMapper.selectByPrimaryKey(
		 * resources.getResTypeId()); magazinesPo.setResTable(
		 * type.getResTable() ); magazinesPo.setResTypeId( type.getResTypeId()
		 * ); magazinesPo.setResources( resources ); Magazines
		 * magazines=this.magazinesMapper.selectByPrimaryKey(
		 * magazinesTableVo.getResId() ); magazinesPo.setMagazines( magazines );
		 * 
		 * List<AuditLogs> auditLogsList =
		 * auditLogsMapper.getAuditLogsListByResId(magazinesTableVo.getResId());
		 * if(null!=auditLogsList&&auditLogsList.size()>0)
		 * magazinesPo.setAuditLogs( auditLogsList.get(0 ) );
		 * List<ResCategoryRelationsVo>
		 * resCategoryRelationsVoList=this.resCategoryRelationsMapper
		 * .getResCategoryRelationsByResId(magazinesTableVo.getResId() );
		 * magazinesPo.setResCategoryRelationsList( resCategoryRelationsVoList
		 * );
		 * 
		 * 
		 * List<SpecialResourceDatasVo> tempSpecialResDataList =
		 * specialResourceDatasMapper
		 * .getSpecialResourceDataByResId(magazinesTableVo.getResId());
		 * 
		 * 
		 * magazinesPo.setSpecialResourceDatasVoList( tempSpecialResDataList );
		 * 
		 * 
		 * magazinesListPo.add(magazinesPo );
		 * 
		 * } try { MagazinesMongo.addMagazines( magazinesListPo ); } catch
		 * (Exception e) { System.out.println( "緩存添加失敗" ); } }
		 */
	}

}